
    var trainPlan=new Object();
    trainPlan.roles="";
    trainPlan.deptIds=[];
    trainPlan.planId='';
    trainPlan.planId=$("#id").val();
    trainPlan.init=function(basePath){
        trainPlan.basePath=basePath;
            //选择单位
        trainPlan.initDepartment();

        window.groupTreeCheckbox_search = trainPlan.groupTreeChecked;
    }

    //子节点取消选中
    trainPlan.dis_node = function (treeObj,node) {
        treeObj.checkNode(node, false, false, true);
        var children = node.children;
        if(children){
            for(var j=0; j< children.length; j++) {
                trainPlan.dis_node(treeObj,children[j]);
            }
        }
    };

    //将子节点选中
    trainPlan.slelect_node = function (treeObj,node) {
        treeObj.checkNode(node, true, true, true);
        var children = node.children;
        if(children){
            for(var j=0; j< children.length; j++) {
                trainPlan.slelect_node(treeObj,children[j]);
            }
        }
    };

    trainPlan.groupTreeChecked  = function(treeObj,treeNode){
        if(!$("#isChecked").is(":checked")){
            //取消子节点选中状态
            var nodes = treeNode.children;
            if(nodes){
                for(var i=0; i< nodes.length; i++) {
                    if(nodes[i] ){
                        trainPlan.dis_node(treeObj,nodes[i]);
                    }
                }
            }
        }
    }

    trainPlan.groupTreeUnChecked = function (treeObj,treeNode) {
        var nodes = treeNode.children;
        if(nodes){
            for(var i=0; i< nodes.length; i++) {
                if(nodes[i]){
                    trainPlan.slelect_node(treeObj,nodes[i]);
                }
            }
        }
    }

    trainPlan.checkNodes = function () {
        var treeObj = $.fn.zTree.getZTreeObj("group_tree");
        var checkedNodes = treeObj.getCheckedNodes(true);
        if(checkedNodes.length==0){
            return;
        }
        for(var i=0; i< checkedNodes.length; i++) {
            if(!$("#isChecked").is(":checked")){
                trainPlan.groupTreeChecked(treeObj, checkedNodes[i]);
            }else{
                trainPlan.groupTreeUnChecked(treeObj, checkedNodes[i]);
            }

        }

    }


     //初始化培训范围列表
    trainPlan.initDepartment=function() {
      $("#department_select2").select2();
      $.ajax({
          url: appPath+ '/admin/project_create/private/trainDepartment',
          dataType: 'json',
          type:'get',
          success:function (data) {
              var list=data.result;
              $("#department_select2").select2({
                  tags:true,
                  data:list,
                  multiple:true,
                  placeholder:"请选择培训范围",
                  language:"zh-CN",
                  readonly:true
              })

              //发送异步请求项目下已有的单位
              $.ajax({
                  url: appPath + '/admin/trainPlan/trainCompanys',
                  dataType: 'json',
                  type:'get',
                  data:{"planId":trainPlan.planId},
                  success:function (data) {
                      var list=data.result;

                      var len=list.length;
                      var inner = "", item;
                      // 组装数据
                      for(var i=0; i< len; i++) {
                          item = list[i];
                          trainPlan.deptIds.push(item.deptId)
                      }
                      $("#department_select2").val(trainPlan.deptIds).trigger("change");
                      trainPlan.initData(data);
                      //点击小X触发，取消树中节点的勾选状态
                      trainPlan.deleteDept();


                  }

              })
          }
      })
    }

    trainPlan.initData = function (data) {
        var result = data.result;
        var treeObj = $.fn.zTree.getZTreeObj("group_tree");
        //初始化默认所选单位选中状态
        for(var i = 0; i< result.length ;i++){
            var companyId = result[i].receiveId;
            var deptId = result[i].deptId;
            var node = treeObj.getNodeByParam("id", deptId, null);
            if(node!=null){
                node.checked = true ;
                treeObj.updateNode(node);
            }

        }
    }

    trainPlan.selectDept=function(){
        $("#department_select2").select2("close");
        //得到最新的培训范围数据
        var departments = $("#department_select2").select2("data");
        trainPlan.getNewDate(departments);
        $('.por').addClass('showTrain');

    };
    //隐藏树下拉框
    trainPlan.close = function(event){
        $('.por').removeClass('showTrain');
        return false;
    };

    // 确定培训范围
    trainPlan.finish = function(){

        var arr_ = [];
        var treeObj = $.fn.zTree.getZTreeObj("group_tree");
        var checkedNodes = treeObj.getCheckedNodes(true);

        if(checkedNodes.length==0){
            layer.msg("请选择培训范围",{time : 1000});
            return false;
        }
        for(var i=0; i<checkedNodes.length; i++){
            var companyId = checkedNodes[i].companyId;
            var departmentId = checkedNodes[i].departmentId;
            var name = checkedNodes[i].name;
            if(checkedNodes[i].departmentId == undefined || '' == checkedNodes[i].departmentId ){
                arr_.push(checkedNodes[i].companyId);
            }else{
                arr_.push(checkedNodes[i].departmentId);
            }

        }
        /*if("trainPlan"==value){
         parent.trainPlan.getDept(arr_);
         }*/
        $("#department_select2").val(arr_).trigger("change");
        $('.por').removeClass('showTrain');
        return false;
    }
    var cognateProjectCount =$("#cognateProjectCount").val().trim();

    //点击小X触发，取消树中节点的勾选状态
    if (cognateProjectCount>0){
    trainPlan.deleteDept = function () {
        $(document).delegate('.select2-selection__choice__remove','click',function(){
            // 禁用培训范围下拉框
            $("#department_select2").select2("close");
            var id = $(this).parent().attr('id');
            if (trainPlan.deptIds.indexOf(id)>-1){
                layer.alert("不能删除已有受训单位！")
                $("#department_select2").val(trainPlan.deptIds).trigger("change");
            }else{
                var treeObj = $.fn.zTree.getZTreeObj(tree_options.id);
                var node = treeObj.getNodeByParam("id", id, null);
                node.checked = false ;
                treeObj.updateNode(node);
            }
        })
       }
    }else{
        trainPlan.deleteDept = function () {
            $(document).delegate('.select2-selection__choice__remove','click',function(){
                // 禁用培训范围下拉框
                $("#department_select2").select2("close");
                var id = $(this).parent().attr('id');
                var treeObj = $.fn.zTree.getZTreeObj(tree_options.id);
                var node = treeObj.getNodeByParam("id", id, null);
                node.checked = false ;
                treeObj.updateNode(node);

            })
        }
    }

    //弹窗选择部门后回调
    trainPlan.getDept = function (depatements) {
        // 组装数据
        for(var i=0; i< depatements.length; i++) {
            if(trainPlan.repeatDept(depatements[i],trainPlan.deptIds)){
                trainPlan.deptIds.push(depatements[i])
            }
        }

    }

    //封装角色和培训范围数据
    trainPlan.assembleDate=function(datas){
          var sbingDatas=[];
              for(var i = 0;i<datas.length;i++){
                var object = new Object();
                object.id = datas[i].id;
                object.text = datas[i].text;
                object.type = datas[i].type;
                sbingDatas.push(object)
              }
          return JSON.stringify(sbingDatas);
     }


     //得到最新的培训范围数据
    trainPlan.getNewDate=function(datas){
        var deptDatas = [];
        for(var i = 0;i<datas.length;i++){
            deptDatas.push(datas[i].id);
        }
        trainPlan.deptIds = deptDatas;
    }

    //去除重复的培训范围
    trainPlan.repeatDept=function(id,depatements){
        var flag=true;
        for (var i=0;i<depatements.length;i++){
            if(depatements[i]==id){
                flag=false;
            }
        }
        return flag;
    }